package com.test.burnincash.activity_fragment_burnin.fragment_burnin;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.test.burnincash.R;
import com.test.burnincash.activity_fragment_burnin.UsagePermissionGuideActivity_burnin;
import com.test.burnincash.activity_fragment_burnin.base_burnin.BaseActivity_burnin;
import com.test.burnincash.activity_fragment_burnin.base_burnin.BaseApplication_burnin;
import com.test.burnincash.activity_fragment_burnin.base_burnin.BaseLoanFragment_burnin;
import com.test.burnincash.bean_burnin.config_info_burnin.AppShowInfo_burnin;
import com.test.burnincash.bean_burnin.user_info_burnin.LoanLimitInfo_burnin;
import com.test.burnincash.constant_burnin.Constant_burnin;
import com.test.burnincash.dialog_burnin.CustomerServiceDialog_burnin;
import com.test.burnincash.util_burnin.PermissionUtil_burnin;
import com.test.burnincash.util_burnin.StringUtil_burnin;
import com.test.burnincash.util_burnin.helper_burnin.UserInfoHelper_burnin;
import com.test.burnincash.util_burnin.http_burnin.HttpManager_burnin;
import com.test.burnincash.util_burnin.analytics_burnin.util_burnin.AnalyticsUtil_burnin;

import pub.devrel.easypermissions.EasyPermissions;

public class LoanMainFragment_burnin extends BaseLoanFragment_burnin {

    private TextView loanAmountTv;

    public boolean isRefresh = false;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_loan_main_burnin, container, false);
        init_burnin(view);
        return view;
    }

    @SuppressLint("SetTextI18n")
    private void init_burnin(View view) {
        loanAmountTv = view.findViewById(R.id.fragment_loan_main_tv_loan_amount);

        TextView limit1Tv = view.findViewById(R.id.fragment_loan_main_tv_limit_1);
        TextView limit2Tv = view.findViewById(R.id.fragment_loan_main_tv_limit_2);
        String moneyUnit = Constant_burnin.Money_Unit_Nigeria;
        limit1Tv.setText(moneyUnit + " 800,000");
        limit2Tv.setText(moneyUnit + " 1,000,000");

        view.findViewById(R.id.fragment_loan_main_btn_refresh_status).setOnClickListener(v -> {
            isRefresh = true;
            HttpManager_burnin.getInstance().requestUserProcess();
        });

        view.findViewById(R.id.fragment_loan_main_layout_apply_btn).setOnClickListener(v -> {
            HttpManager_burnin.getInstance().saveUserBuriedPoint((this.getClass().getSimpleName() + "_CLICK").toUpperCase());
            if (PermissionUtil_burnin.checkLaunchPermissions(requireActivity(), AppShowInfo_burnin.getInstance().getHome())) {
                //device info upload
                try {
                    if (UserInfoHelper_burnin.getInstance().didLogin()) {
                        AnalyticsUtil_burnin.INSTANCE.saveDeviceS2SInfo();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

                //request usage stats permission
                if (AppShowInfo_burnin.getInstance().needRequestUsageStatsPermission()) {
                    AnalyticsUtil_burnin.INSTANCE.checkUsageStatsPermission(BaseApplication_burnin.getApplication_burnin(), permissionGrant -> {
                        if (permissionGrant) {
                            //已经同意权限
                            //继续走业务流程
                            //all done, request user state
                            HttpManager_burnin.getInstance().requestUserProcess();

                        } else {
                            //不同意权限
                            //1 展示UI部分--》引导1--》引导2
                            //....
                            //....
                            Activity activity = requireActivity();
                            if (requireActivity() instanceof BaseActivity_burnin)
                                ((BaseActivity_burnin)activity).startActivity_burnin(UsagePermissionGuideActivity_burnin.class);
                        }
                        return null;
                    });
                } else {
                    //all done, request user state
                    HttpManager_burnin.getInstance().requestUserProcess();
                }
            } else {
                EasyPermissions.requestPermissions(this, getString(R.string.launch_permissions), PermissionUtil_burnin.REQ_CODE_PERMISSIONS, AppShowInfo_burnin.getInstance().getHome());
            }
        });

        view.findViewById(R.id.fragment_loan_main_ib_customer_service).setOnClickListener(v -> new CustomerServiceDialog_burnin(requireContext()).show());

        HttpManager_burnin.getInstance().queryLoanLimit();

    }

    @SuppressLint("SetTextI18n")
    public void updateLoanAmount_burnin() {
        isRefresh = false;
        loanAmountTv.setText(StringUtil_burnin.getSafeString(Constant_burnin.Money_Unit_Nigeria).toUpperCase() + " "
                + LoanLimitInfo_burnin.getInstance().getLimitAmount());
    }

}